package com.jxzyk.model.found;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.renderscript.Type.CubemapFace;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.adapter.Holder;
import com.core.entity.I_List;
import com.core.ui.fragment.BaseFragment;
import com.core.util.T;
import com.jxzyk.App;
import com.jxzyk.R;
import com.jxzyk.adapter.CourseAdapter;
import com.jxzyk.adapter.GridAdapter;
import com.jxzyk.adapter.GridAdapter.OnGridClickListener;
import com.jxzyk.config.API;
import com.jxzyk.entity.Course;
import com.jxzyk.entity.CourseType;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.Query;
import com.jxzyk.http.req.CourseRequest;
import com.jxzyk.model.MainActivity;
import com.jxzyk.model.course.CourseActivity;
import com.jxzyk.model.course.CourseFragment;
import com.jxzyk.util.BitmapHelp;
import com.jxzyk.util.Utility;
import com.jxzyk.view.ScrollViewExtend;
import com.jxzyk.view.TitleLayout;
import com.jxzyk.view.imageindicator.AutoPlayManager;
import com.jxzyk.view.imageindicator.ImageIndicatorView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class FoundFragment extends BaseFragment {
	@ViewInject(R.id.indicate_view)
	ImageIndicatorView autoImageIndicatorView;
	@ViewInject(R.id.title)
	TitleLayout title;
	// @ViewInject(R.id.vp_type)
	// ViewPager vp_type;
	@ViewInject(R.id.list_found)
	ListView listView;
	private CourseRequest request;
	BitmapUtils bitmapUtils;
	@ViewInject(R.id.sc_found)
	ScrollViewExtend sc_found;

	@Override
	public void initData(Bundle bundle) {
		bitmapUtils = BitmapHelp.getBitmapUtils(getActivity());
	}

	@Override
	public int initUiRes() {
		return R.layout.fra_found;
	}

	private List<I_List> types;

	public void setupAutoPager(String[] urls) {
		// final Integer[] resArray = new Integer[] { R.drawable.ic_next,
		// R.drawable.ic_launcher, R.drawable.ic_next };
		this.autoImageIndicatorView.setupLayoutByUrl(urls);
		// this.autoImageIndicatorView.setupLayoutByDrawable(resArray);
		this.autoImageIndicatorView.show();
		AutoPlayManager autoBrocastManager = new AutoPlayManager(
				this.autoImageIndicatorView);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(5);// 循环次数
		autoBrocastManager.setBroadcastTimeIntevel(3 * 1000, 3 * 1000);// 首次启动时间及间隔
		autoBrocastManager.loop();
	}

	@Override
	public void initUi(View view) {
		title.setTtile("智慧职教");
		String[] urls = new String[] {
				"http://210.28.101.76:8080/doc/image/index/0.jpg",
				"http://210.28.101.76:8080/doc/image/index/1.jpg",
				"http://210.28.101.76:8080/doc/image/index/2.jpg" };
		setupAutoPager(urls);

		// 加载课程类别
		// request = new CourseRequest(getActivity(), API.Course.type);
		// request.setOnDialogListener(new OnDialogListener() {
		//
		// @Override
		// public void onSuccess(JSONObject json) {
		// try {
		// // net_found.stop();
		// types = new ArrayList<>();
		// JSONArray array = json.getJSONArray("data");
		// App app = (App) getActivity().getApplication();
		// app.setCourseType(array.toJSONString());
		// for (int i = 0; i < array.size(); i++) {
		// CourseType type = (CourseType) new CourseType()
		// .instance(array.getJSONObject(i));
		// types.add(type);
		// }
		// initGridData(types);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		//
		// @Override
		// public void onFailure(String msg) {
		// // net_found.stop();
		// }
		// });
		// // net_found.setRequest(request);
		// // net_found.start();
		// request.start();
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bundle bundle = new Bundle();
				try {
					Course course = (Course) adapter.getLists().get(arg2);
					bundle.putSerializable(course.getClass().getName(), course);
					showActivity(CourseActivity.class, bundle);
				} catch (Exception e) {
					e.printStackTrace();
					T.toast("下标越界[listsize]" + lists.size() + "[index]" + arg2);
				}
			}
		});
		loadList(0);
	}

	private int scrollY = 0;

	@Override
	public void onPause() {
		super.onPause();
		scrollY = sc_found.getScrollY();
	}

	@Override
	public void onResume() {
		super.onResume();
		if (listView != null) {
			sc_found.smoothScrollTo(0, scrollY);
		}
	}

	private void initGridData(final List<I_List> lists) {
		final GridAdapter adapter = new GridAdapter(getActivity(), lists, 4, 4);
		adapter.setOnGridClickListener(new OnGridClickListener() {

			@Override
			public void onItemClick(int i) {
				MainActivity activity = (MainActivity) getActivity();
				CourseType tp = (CourseType) lists.get(i);
				CourseFragment courseFragment = (CourseFragment) activity.courseFragment;
				courseFragment.courseType = tp;
				activity.viewPager.setCurrentItem(1);
			}

			Holder holder;

			@Override
			public View createView(LayoutInflater mInflater, int position,
					View convertView, ViewGroup parent) {
				CourseType type = (CourseType) lists.get(position);
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.item_grid, null);
					holder = new Holder();
					convertView.setTag(holder);
				} else {
					holder = (Holder) convertView.getTag();
				}
				holder.img1 = (ImageView) convertView
						.findViewById(R.id.img_item_grid);
				holder.tv1 = (TextView) convertView
						.findViewById(R.id.tv_item_grid);

				// 加载网络图片
				bitmapUtils.display(holder.img1, type.getType_image());
				holder.tv1.setText(type.getType_name());
				return convertView;
			}
		});
		// vp_type.setAdapter(adapter);
	}

	private List<I_List> lists = new ArrayList<>();
	private CourseAdapter adapter = null;
	private String type = null;

	private void loadList(int my_page) {
		CourseRequest request = new CourseRequest(getActivity(),
				API.Course.index);
		Query query = new Query();
		query.setPage(my_page);
		query.setLength(5);
		request.setType(type);
		request.setQuery(query);
		if (my_page == 0) {
			lists.clear();
			if (adapter == null) {
				adapter = (CourseAdapter) new CourseAdapter().instance(
						getActivity(), lists);
				listView.setAdapter(adapter);
				Utility.setListViewHeightBasedOnChildren(listView);
			}
		}
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					JSONArray array = json.getJSONArray("data");
					if (array.size() > 0) {
						String[] urls = new String[array.size()];
						for (int i = 0; i < array.size(); i++) {
							Course course = (Course) new Course()
									.instance(array.getJSONObject(i));
							urls[i] = course.getCourse_img();
							lists.add(course);
						}
						// setupAutoPager(urls);
						adapter.notifyDataSetChanged();
						sc_found.smoothScrollTo(0, scrollY);
						// page++;
					}
					// Call onRefreshComplete when the list has been refreshed.
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				// Call onRefreshComplete when the list has been refreshed.
				adapter.notifyDataSetChanged();
				// if (page == 0) {
				// T.toast("数据为空");
				// }
			}
		});
		request.start();
	}
}
